define(['jquery', 'bootstrap', 'backend', 'table','form'], function ($, undefined, Backend, Table,Form) {
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'mlogs/index',
                    add_url: 'mlogs/send',
                    edit_url: '',
                    del_url: 'mlogs/del',
                    multi_url: '',
                }
            });
            var table = $("#table");
            var ids = [];
            table.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table', function (e, row) {
                if (e.type == 'check' || e.type == 'uncheck') {
                    row = [row];
                } else {
                    ids = [];
                }
                $.each(row, function (i, j) {
                    if (e.type.indexOf("uncheck") > -1) {
                        var index = ids.indexOf(j.id);
                        if (index > -1) {
                            ids.splice(index, 1);
                        }
                    } else {
                        ids.indexOf(j.id) == -1 && ids.push(j.id);
                    }
                });
                console.log(ids)
                $("#table").attr('data-aid',ids);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                columns: [
                    [
                        {field: 'state', checkbox: true},
                        {field: 'id', title: 'ID',operate:false,width:"70px"},
                        {field: 'member.headimgurl',width:"45px" ,title: __('头像'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'member.nickname', title: __('昵称'),align:'left'},
                        {field: 'msg_type', title: __('类型'),align:'left'},
                        {field: 'content', title: __('内容'),align:'left',formatter:Controller.formatter.content},
                        {field: 'create_time',width:"145px", title: __('更新时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'operate',width:"125px",align:'right', title: __('Operate'), table: table,
                            events: Table.api.events.operate,
                            
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            $("body").on('click',".img-view",function(){
                var data = [];
                Layer.photos({
                    photos:".view-img",
                    anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
                });
            })
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        send:function(){
            var table = $("#table",window.parent.document);
            var ids = table.data("aid");
            if(ids == '' || ids == null || ids == undefined){
                Fast.api.close();
                Toastr.error("请选择回复的消息.");
                return false;
            }
            $("#_ids").val(ids)
            $("body").on('click',"input[name='row[type]']",function(){
                $("._content").hide();
                $(".content-"+$(this).val()).show();
            })
            Form.api.bindevent($("form[role=form]"));
        },
        formatter:{
            content:function(value, row, index){
                if(row.msg_type == 'text'){
                    return value;
                }else if(row.msg_type == 'image'){
                    return "<div class='view-img'><img src='"+row.url+"' width='60' class='img-view'/></div>"
                }
            }
        }
        
    };
    return Controller;
});
